# coding: utf-8 
# @Time : 2021/5/13 14:24
# @Author : shenshaoxiong
# @FileName: GetHTML2db.py
# @Email   : 765105236@qq.com

from .Constant import HOST, USER, PASSWORD, DB, PORT, log_table, log_table_field_list
from .Funcs import get_response
from .Funcs import get_mysql_data
from .Funcs import insert_mysql
from .Funcs import update_data
from .Funcs import get_now_time


def get_html_2_db(source=None, size=None):
    select_table = "news_url_list"
    filter = "source='%s' and state=0" % source
    select_field_list = ["id", "url"]
    # 结果表
    insert_table = "news_detail_html"
    insert_vaules_list = []
    insert_field_list = ["id", "source", "url", "html", "state", "insert_time"]
    # 日志
    log_list = []
    # 更新数据
    update_values_list = []
    # 取数据
    task_data_list = get_mysql_data(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, table=select_table,
                                    field_list=select_field_list, size=size, filter=filter)
    for task_data in task_data_list:
        id = task_data["id"]
        url = task_data["url"]
        response = get_response(url)
        html = response["text"]
        response_state = response["response_state"]
        response_info = response["response_info"]
        insert_time = get_now_time()
        insert_vaules_list.append((id, source, url, html, 0, insert_time))
        log_list.append((id, response_state, source, response_info, url, insert_time))
        update_values_list.append((1, id))

    insert_mysql(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, table=insert_table,
                 field_list=insert_field_list, values_list=insert_vaules_list)
    insert_mysql(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, table=log_table,
                 field_list=log_table_field_list, values_list=log_list)
    update_sql = "UPDATE news_url_list SET state = %s WHERE id = %s"
    update_data(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, sql=update_sql, values_list=update_values_list)


# if __name__ == "__main__":
#     get_html_2_db(source="新浪国内", size=5)